<?php

use Illuminate\Database\Capsule\Manager as DB;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

#[店铺订单中心]
class Dianpu_OrderController extends AcommonController
{

    public function init()
    {
        $this->model      = (new orderModel())->newQueryWithoutScope('dianpu_id')->where('dianpu_id', '>', 0);
        $this->table      = 'order';
        $this->primaryKey = 'id';
        parent::init();
    }

    public function indexAction($returnData = FALSE)
    {
        if ($this->isPost()) {
            $this->appends = ['status_code', 'status_name'];
            $this->model   = $this->model->with(['member', 'customer', 'item', 'dianpu']);
            $start_on      = $this->get('start_on');
            if (!empty($start_on)) {
                array_push($this->condition, ['created_at', '>=', $start_on]);
            }
            $end_on = $this->get('end_on');
            if (!empty($end_on)) {
                array_push($this->condition, ['created_at', '<=', $end_on]);
            }
            $mode = $this->get('mode');
            switch ($mode) {
                case 'canceled':
                    $this->model = $this->model->where('status', orderModel::STATUS_CANCEL);
                    break;
                case 'closed':
                    $this->model = $this->model->where('status', orderModel::STATUS_INVALID);
                    break;
                case 'nopay':
                    $this->model = $this->model->where('status', orderModel::STATUS_NOPAY);
                    break;
                case 'paid':
                    $this->model = $this->model->where('status', orderModel::STATUS_PAYED);
                    break;
                case 'nosend':
                    $this->model = $this->model->where('status', orderModel::STATUS_PAYED)->whereExists(function ($query) {
                        $query->from('order_item')->whereColumn('order_item.order_id', 'order.id')
                            ->where('dispatch_status', orderitemModel::DISPATCH_STATUS_NOSEND)
                            ->whereNotIn('refund_status', [orderitemModel::REFUND_STATUS_OK, orderitemModel::REFUND_STATUS_FINISH])
                            ->whereNotIn('aftersale_status', [orderitemModel::AFTERSALE_STATUS_AFTERING, orderitemModel::AFTERSALE_STATUS_OK]);
                    });
                    break;
                case 'send':
                    $this->model = $this->model->where('status', orderModel::STATUS_PAYED)->whereExists(function ($query) {
                        $query->from('order_item')->whereColumn('order_item.order_id', 'order.id')
                            ->where('dispatch_status', orderitemModel::DISPATCH_STATUS_SENDED)
                            ->whereNotIn('refund_status', [orderitemModel::REFUND_STATUS_OK, orderitemModel::REFUND_STATUS_FINISH])
                            ->whereNotIn('aftersale_status', [orderitemModel::AFTERSALE_STATUS_AFTERING, orderitemModel::AFTERSALE_STATUS_OK]);
                    });
                    break;
                case 'nocomment':
                    $this->model = $this->model->where('status', orderModel::STATUS_PAYED)->whereExists(function ($query) {
                        $query->from('order_item')->whereColumn('order_item.order_id', 'order.id')
                            ->where('dispatch_status', orderitemModel::DISPATCH_STATUS_GETED)
                            ->where('comment_status', orderitemModel::COMMENT_STATUS_NO)
                            ->whereNotIn('refund_status', [orderitemModel::REFUND_STATUS_OK, orderitemModel::REFUND_STATUS_FINISH])
                            ->whereNotIn('aftersale_status', [orderitemModel::AFTERSALE_STATUS_AFTERING, orderitemModel::AFTERSALE_STATUS_OK]);
                    });
                    break;
                case 'finished':
                    $this->model = $this->model->where('status', orderModel::STATUS_FINISH);
                    break;
                case 'aftersale':
                    $this->model = $this->model->where('status', orderModel::STATUS_PAYED)->whereExists(function ($query) {
                        $query->from('order_item')->whereColumn('order_item.order_id', 'order.id')
                            ->where('aftersale_status', '<>', orderitemModel::AFTERSALE_STATUS_NOAFTER);
                    });
                    break;
                case 'refund':
                    $this->model = $this->model->where('status', orderModel::STATUS_PAYED)->whereExists(function ($query) {
                        $query->from('order_item')->whereColumn('order_item.order_id', 'order.id')
                            ->where('refund_status', '<>', orderitemModel::REFUND_STATUS_NOREFUND);
                    });
                    break;
            }
            $keywordsType = $this->get('keywordsType');
            $keywords     = $this->get('keywords');
            if ($keywords !== '') {
                switch ($keywordsType) {
                    case 'dianpu_id':
                        $this->model = $this->model->where('dianpu_id', $keywords);
                        break;
                    case 'order_id':
                        $this->model = $this->model->where('id', $keywords);
                        break;
                    case 'order_sn':
                        $this->model = $this->model->where('order_sn', 'like', "%{$keywords}%");
                        break;
                    case 'member_id':
                        $this->model = $this->model->where('member_id', $keywords);
                        break;
                    case 'member_phone':
                        $this->model = $this->model->whereIn('member_id', DB::table('member')->where('phone', 'like', "%{$keywords}%")->limit(20)->pluck('id'));
                        break;
                    case 'customer_id':
                        $this->model = $this->model->where('customer_id', $keywords);
                        break;
                    case 'consignee':
                        $this->model = $this->model->where('consignee', $keywords);
                        break;
                    case 'consignee_phone':
                        $this->model = $this->model->where('phone', 'like', "%{$keywords}%");
                        break;
                }
            }
            $payType = $this->get('payType');
            if ($payType !== '') {
                $this->model = $this->model->where('pay_type', $payType);
            }
            $goods = $this->get('goods');
            if ($goods !== '') {
                if (is_numeric($goods)) {
                    $this->model = $this->model->whereExists(function ($query) use ($goods) {
                        $query->from('order_item')->whereColumn('order_item.order_id', 'order.id')->where('goods_id', $goods);
                    });
                } else {
                    $this->model = $this->model->whereExists(function ($query) use ($goods) {
                        $query->from('order_item')->whereColumn('order_item.order_id', 'order.id')
                            ->whereIn('goods_id', DB::table('goods')->where('dianpu_id','=',0)->where('title', 'like', "%{$goods}%")->limit(20)->pluck('id'));
                    });
                }
            }
            $dataset = parent::indexAction(TRUE);
            foreach ($dataset['rows'] as $k1 => &$v1) {
                if($v1['ext']){
                    $ext = json_decode($v1['ext'], true);
                    if($ext['combo']==1){
                        $v1['combo_goods_id'] = $v1['item'][0]['goods_id'];
                        $v1['combo_goods_title'] = $v1['item'][0]['goods_title'];
                    }
                }
                foreach ($v1['item'] as $k2 => $v2) {
                    if ($v2['goods_combo']) {
                        $combo = json_decode($v2['goods_combo'], true);
                        if($combo['goods']) {
                            foreach ($combo['goods'] as $k3 => &$v3) {
                                $v3['status_code']      = $v3['status_code'] ?? $v2['status_code'];
                                $v3['refund_status']    = $v3['refund_status'] ?? 0;
                                $v3['aftersale_status'] = $v3['aftersale_status'] ?? 0;
                            }
                            $v1['item'] = $combo['goods'] ?? [];
                        }
                    }
                }
            }
            json($dataset);
        } else {
            parent::indexAction();
        }
    }
}
